﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace SpaceInvaders001
{
    class Casamata
    {
        private Texture2D imagem;
        private Vector2 posicao;

        int[,] map = new int[,] { {0,1,1,1,1,1,1,1,1,1,1,1,1,0}, 
                                  {1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 
                                  {1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 
                                  {1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 
                                  {1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 
                                  {1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 
                                  {1,1,0,0,0,0,0,0,0,0,0,0,1,1}, 
                                  {1,1,0,0,0,0,0,0,0,0,0,0,1,1} };

        private class Bloco
        {
            public Rectangle rect;
            public bool naTela = true;
            public Vector2 pos;

        }

        List<Bloco> blocos = new List<Bloco>();

        public Casamata(Texture2D imagem, Vector2 posicao)
        {
            this.imagem = imagem;
            this.posicao = posicao;

            for (int x = 0; x < map.GetLength(0); x++)
            {
                for (int y = 0; y < map.GetLength(1); y++)
                {
                    if(map[x, y]==1){
                        this.blocos.Add(new Bloco());
                        this.blocos[this.blocos.Count - 1].rect = new Rectangle((int)this.posicao.X + (y * (this.imagem.Width + 0)), (int)this.posicao.Y + (x * (this.imagem.Height + 0)), this.imagem.Width, this.imagem.Height);
                        this.blocos[this.blocos.Count - 1].pos.X = x;
                        this.blocos[this.blocos.Count - 1].pos.Y = y;
                    }
                }
            }
            
        }


        public void draw(GameTime gameTime, SpriteBatch spriteBatch)
        {

            foreach (Bloco b in this.blocos)
            {
                if (b.naTela)
                {
                    spriteBatch.Draw(this.imagem, new Vector2(b.rect.X, b.rect.Y),
                                           new Rectangle(0, 0, this.imagem.Bounds.Width,
                                           this.imagem.Bounds.Height),
                                           Color.White, 0f, Vector2.Zero,
                                           1f,
                                           SpriteEffects.None,
                                           0.1f
                                           );
                }
            }
        }

        public void verificaColisao(ref List<Tiro> tiros)
        {

            /* COLISÃO Entre os Tiros e os Inimigos*/
            for (int t = 0; t < tiros.Count; t++)
            {

                //tiros[t].update(gameTime);

                for (int i = 0; i < this.blocos.Count; i++)
                {
                    if (this.blocos[i].rect.Intersects(tiros[t].rect) && tiros[t].naTela && this.blocos[i].naTela)
                    {
                        //this.blocos.RemoveAt(i);
                        this.blocos[i].naTela = false;

                        Circle c = new Circle(tiros[t].posicao, tiros[t].radius);
                        for (int j = 0; j < this.blocos.Count; j++)
                        {
                            if (this.blocos[j].naTela && c.Intersects(this.blocos[j].rect))
                            {
                                this.blocos[j].naTela = false;
                            }
                        }

                        /*
                         * int tmp = (int)((this.blocos[i].pos.X * map.GetLength(0)) + this.blocos[i].pos.Y);
                         * Console.WriteLine("i=" + i.ToString() + ",  map.GetLength(0)=" + map.GetLength(0).ToString() + ", tmp=" + tmp.ToString() + ", x=" + this.blocos[i].pos.X + ", y=" + this.blocos[i].pos.Y);
                         * 
                         */



                        tiros.RemoveAt(t);
                        return;
                    }
                }

                if (!tiros[t].naTela)
                {
                    tiros.RemoveAt(t);
                }
            }

        }

    }
}
